package com.zxjbyte.yiyi.framework.common.domain.mail;

import cn.hutool.core.util.ObjectUtil;
import lombok.Data;

/**
 * 邮件元数据
 * @Author zhangxingjia
 * @Date 2023/5/25 10:42
 * @Version: 1.0
 */
@Data
public class MailMetadata {

    // 发件人
    private String from;
    // 收件人
    private String[] to;
    // 抄送人
    private String[] cc;
    // 密送人
    private String[] bcc;
    // 主题
    private String subject;
    // 内容
    private String text;
    // 是否解析html
    private boolean html = false;

    public MailMetadata(){
    }

    public MailMetadata(String from, String subject, String text){
        this.from = from;
        this.subject = subject;
        this.text = text;
    }

    public void setTo(String to) {
        this.to = new String[]{to};
    }

    public void setTo(String... to) {
        this.to = to;
    }

    public void setCc(String cc) {
        this.cc = new String[]{cc};
    }

    public void setCc(String... cc) {
        this.cc = cc;
    }

    public void setBcc(String bcc) {
        this.bcc = new String[]{bcc};
    }

    public void setBcc(String... bcc) {
        this.bcc = bcc;
    }

    /**
     * 创建邮件元数据静态方法
     * 注意：参数to, cc, bcc 类型只支持String和String[], 否则抛出IllegalArgumentException异常
     * @return MailMetadata
     */
    public static MailMetadata of(String from, Object to, Object cc,
                                  Object bcc, String subject, String text){
        MailMetadata metadata = new MailMetadata(from, subject, text);
        metadata.setTo(object2StrArr(to));
        metadata.setCc(object2StrArr(cc));
        metadata.setBcc(object2StrArr(bcc));
        return metadata;
    }

    protected static String[] object2StrArr(Object o){
        if(ObjectUtil.isNull(o)){
            return null;
        }
        if(o instanceof String){
            return new String[]{(String) o};
        } else if(o instanceof String[]){
            return (String[]) o;
        } else {
            throw new IllegalArgumentException("Unsupported parameter type");
        }
    }
}
